package com.practice.springboot_01_zwj.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageInfo;
import com.practice.springboot_01_zwj.entity.AccountInfo;
import com.practice.springboot_01_zwj.entity.MainTask;
import com.practice.springboot_01_zwj.entity.ProjectReport;
import com.practice.springboot_01_zwj.entity.ReportDetails;
import com.practice.springboot_01_zwj.entity.vo.R;
import com.practice.springboot_01_zwj.service.ProjectReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/projectReport")
public class ProjectReportController {
    @Autowired
    private ProjectReportService projectReportService;

    // 统一的响应结构
    private Map<String, Object> buildResponse(int code, String message, Object data) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", code);
        response.put("message", message);
        if (data != null) response.put("data", data);
        return response;
    }

    @PostMapping("/addProjectReport")
    public ResponseEntity<?> addProjectReport(@RequestBody ProjectReportRequest request){
        try {
            // 可选：在Controller层也进行基础验证
            if (request.getProjectBeginTime() != null &&
                    request.getProjectEndTime() != null &&
                    request.getProjectEndTime().before(request.getProjectBeginTime())) {
                return ResponseEntity.status(400).body(buildResponse(400, "项目结束时间不能早于项目开始时间", null));
            }
            ProjectReport projectReport = new ProjectReport();
            projectReport.setProjectName(request.getProjectName());
            projectReport.setProjectType(request.getProjectType());
            projectReport.setOverallProgress(request.getOverallProgress());
            projectReport.setWeeklyProgress(request.getWeeklyProgress());
            projectReport.setCurrentProblem(request.getCurrentProblem());
            projectReport.setCoordinateFlag(request.getCoordinateFlag());
            projectReport.setProjectBeginTime(request.getProjectBeginTime());
            projectReport.setProjectEndTime(request.getProjectEndTime());
            projectReport.setFinishProgress(request.getFinishProgress());
            projectReport.setCustomerName(request.getCustomerName());
            projectReport.setCurrentStage(request.getCurrentStage());
            projectReport.setCustomerMgr(request.getCustomerMgr());
            projectReport.setProductMgr(request.getProductMgr());
            projectReport.setDeliveryMgr(request.getDeliveryMgr());
            projectReport.setPartnerName(request.getPartnerName());
            projectReport.setAfterContractAmount(request.getAfterContractAmount());
            projectReport.setPayAmount(request.getPayAmount());
            projectReport.setCompanyName(request.getCompanyName());
            projectReport.setAfterContractProgress(request.getAfterContractProgress());
            projectReport.setAccountManager(request.getAccountManager());

            Integer keyId = projectReportService.createProjectReport(projectReport);
            return ResponseEntity.ok(buildResponse(200, "创建成功", keyId));

        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(400).body(buildResponse(400, e.getMessage(), null));
        } catch (Exception e) {
            return ResponseEntity.status(500).body(buildResponse(500, "创建失败: " + e.getMessage(), null));
        }
    }

    @PutMapping("/updateProjectReport")
    public R<String> updateProjectReport(@RequestBody ProjectReport projectReport){
        try {
            if (projectReport.getKeyId() == null){
                return R.error(400,"更新失败，任务ID不能为空");
            }
            int result = projectReportService.updateProjectReport(projectReport);
            if(result > 0){
                return R.success("更新成功",null);
            }else {
                return R.error("更新失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @DeleteMapping("/deleteProjectReport/{keyId}")
    public R<String> deleteProjectReport(@PathVariable Integer keyId){
        try {
            int result = projectReportService.deleteProjectReport(keyId);
            if(result > 0){
                return R.success("删除成功",null);
            }else {
                return R.error("删除失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }

    @GetMapping("/selectProjectReport")
    public PageInfo<ProjectReport> selectProjectReport(
            @RequestParam(required = false) String projectName,
            @RequestParam(required = false) String projectType,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        return projectReportService.selectProjectReport(projectName, projectType, page, size);
    }
}

class ProjectReportRequest {
    private Integer keyId;
    private String projectName; //项目名称
    private String projectType; //项目主题
    private String overallProgress; //项目总体进展
    private String weeklyProgress; //本周进展
    private String currentProblem; //当前问题及风险
    private String problemProgress; //问题进展
    private String coordinateFlag;  //是否需要领导协调
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date projectBeginTime; //项目开始时间
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date projectEndTime;  //项目截至时间
    private Double finishProgress; //完成进度
    private String customerName;  //客户名称
    private String currentStage;  //当前阶段
    private Double timeProgress;  //时间进度
    private String taskImplementation;   //进展情况（落后、正常、超前）
    private String customerMgr;   //客户负责人
    private String productMgr;    //产品负责人
    private String deliveryMgr;   //交付经理
    private String partnerName;   //合作伙伴
    private Double afterContractAmount;   //后向合同金额(万元)
    private Double payAmount;   //付款金额(万元)
    private String companyName;   //下游厂家
    private String afterContractProgress;   //后向合同进展
    private String accountManager; //客户经理
    private List<ReportDetails> reportDetailsList;

    public Integer getKeyId() {
        return keyId;
    }

    public void setKeyId(Integer keyId) {
        this.keyId = keyId;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getProjectType() {
        return projectType;
    }

    public void setProjectType(String projectType) {
        this.projectType = projectType;
    }

    public String getOverallProgress() {
        return overallProgress;
    }

    public void setOverallProgress(String overallProgress) {
        this.overallProgress = overallProgress;
    }

    public String getWeeklyProgress() {
        return weeklyProgress;
    }

    public void setWeeklyProgress(String weeklyProgress) {
        this.weeklyProgress = weeklyProgress;
    }

    public String getCurrentProblem() {
        return currentProblem;
    }

    public void setCurrentProblem(String currentProblem) {
        this.currentProblem = currentProblem;
    }

    public String getProblemProgress() {
        return problemProgress;
    }

    public void setProblemProgress(String problemProgress) {
        this.problemProgress = problemProgress;
    }

    public String getCoordinateFlag() {
        return coordinateFlag;
    }

    public void setCoordinateFlag(String coordinateFlag) {
        this.coordinateFlag = coordinateFlag;
    }

    public Date getProjectBeginTime() {
        return projectBeginTime;
    }

    public void setProjectBeginTime(Date projectBeginTime) {
        this.projectBeginTime = projectBeginTime;
    }

    public Date getProjectEndTime() {
        return projectEndTime;
    }

    public void setProjectEndTime(Date projectEndTime) {
        this.projectEndTime = projectEndTime;
    }

    public Double getFinishProgress() {
        return finishProgress;
    }

    public void setFinishProgress(Double finishProgress) {
        this.finishProgress = finishProgress;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCurrentStage() {
        return currentStage;
    }

    public void setCurrentStage(String currentStage) {
        this.currentStage = currentStage;
    }

    public Double getTimeProgress() {
        return timeProgress;
    }

    public void setTimeProgress(Double timeProgress) {
        this.timeProgress = timeProgress;
    }

    public String getTaskImplementation() {
        return taskImplementation;
    }

    public void setTaskImplementation(String taskImplementation) {
        this.taskImplementation = taskImplementation;
    }

    public String getCustomerMgr() {
        return customerMgr;
    }

    public void setCustomerMgr(String customerMgr) {
        this.customerMgr = customerMgr;
    }

    public String getProductMgr() {
        return productMgr;
    }

    public void setProductMgr(String productMgr) {
        this.productMgr = productMgr;
    }

    public String getDeliveryMgr() {
        return deliveryMgr;
    }

    public void setDeliveryMgr(String deliveryMgr) {
        this.deliveryMgr = deliveryMgr;
    }

    public String getPartnerName() {
        return partnerName;
    }

    public void setPartnerName(String partnerName) {
        this.partnerName = partnerName;
    }

    public List<ReportDetails> getReportDetailsList() {
        return reportDetailsList;
    }

    public void setReportDetailsList(List<ReportDetails> reportDetailsList) {
        this.reportDetailsList = reportDetailsList;
    }

    public Double getAfterContractAmount() {
        return afterContractAmount;
    }

    public void setAfterContractAmount(Double afterContractAmount) {
        this.afterContractAmount = afterContractAmount;
    }

    public Double getPayAmount() {
        return payAmount;
    }

    public void setPayAmount(Double payAmount) {
        this.payAmount = payAmount;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getAfterContractProgress() {
        return afterContractProgress;
    }

    public void setAfterContractProgress(String afterContractProgress) {
        this.afterContractProgress = afterContractProgress;
    }

    public String getAccountManager() {
        return accountManager;
    }

    public void setAccountManager(String accountManager) {
        this.accountManager = accountManager;
    }
}
